package com.devsoft.foodfrenzy;

import org.andengine.engine.Engine;
import org.andengine.engine.handler.timer.ITimerCallback;
import org.andengine.engine.handler.timer.TimerHandler;
import org.andengine.ui.IGameInterface.OnCreateSceneCallback;

public class SceneManager
{
    private BaseScene splashScene;		//scenes
    private BaseScene mainMenuScene;
    private BaseScene SPMenuScene;
    private BaseScene arcadeScene;
    private BaseScene classicScene;
    private BaseScene mpScene;
    private BaseScene H2HScene;
    private BaseScene optionsScene;
    private BaseScene achievementsScene;
    private BaseScene loadingScene;
    private BaseScene soonScene;
    private BaseScene playerInfoScene;
    private BaseScene gameOverScene;
    private BaseScene titleScene;
    private BaseScene creditsScene;
    
    private static final SceneManager INSTANCE = new SceneManager();
    private SceneType currentSceneType = SceneType.SCENE_SPLASH;
    private BaseScene currentScene;
    private Engine engine = ResourcesManager.getInstance().engine;
    
    
    public enum SceneType
    {
        SCENE_SPLASH,
        SCENE_MAINMENU,
        SCENE_SPMENU,
        SCENE_ARCADE,
        SCENE_CLASSIC,
        SCENE_MULTIPLAYER,
        SCENE_H2H,
        SCENE_OPTIONS,
        SCENE_ACHIEVEMENTS,
        SCENE_LOADING,
        SCENE_SOON,
        SCENE_INFO,
        SCENE_GAMEOVER,
        SCENE_TITLE,
        SCENE_CREDITS
        
    }
    
    
    public void setScene(BaseScene scene)
    {
        engine.setScene(scene);
        currentScene = scene;
        currentSceneType = scene.getSceneType();
        
    }
    
    
    public void setScene(SceneType sceneType)	//used to set scenes
    {
        switch (sceneType)
        {
	        case SCENE_SPLASH:
	            setScene(splashScene);
	            break;
            case SCENE_MAINMENU:
                setScene(mainMenuScene);
                break;
            case SCENE_SPMENU:
                setScene(SPMenuScene);
                break;
            case SCENE_ARCADE:
                setScene(arcadeScene);
                break;
            case SCENE_CLASSIC:
                setScene(classicScene);
                break;
            case SCENE_MULTIPLAYER:
                setScene(mpScene);
                break;
            case SCENE_H2H:
                setScene(H2HScene);
                break;
            case SCENE_OPTIONS:
                setScene(optionsScene);
                break;
            case SCENE_ACHIEVEMENTS:
                setScene(achievementsScene);
                break;
            case SCENE_LOADING:
                setScene(loadingScene);
                break;
            case SCENE_SOON:
                setScene(soonScene);
                break;
            case SCENE_INFO:
                setScene(playerInfoScene);
                break;
            case SCENE_GAMEOVER:
                setScene(gameOverScene);
                break;
            case SCENE_TITLE:
                setScene(titleScene);
                break;
            case SCENE_CREDITS:
                setScene(creditsScene);
                break;
            default:
                break;
                
        }
        
    }
    
    
    public void createSplashScene(OnCreateSceneCallback pOnCreateSceneCallback)
    {
        ResourcesManager.getInstance().loadSplashScreen();
        splashScene = new SplashScene();
        currentScene = splashScene;
        pOnCreateSceneCallback.onCreateSceneFinished(splashScene);
        
    }
    
    
    private void disposeSplashScene()
    {
        ResourcesManager.getInstance().unloadSplashScreen();
        splashScene.disposeScene();
        splashScene = null;
        
    }
    
    public void createTitleScene()
    {
        ResourcesManager.getInstance().loadTitleResources();
        titleScene = new TitleScene();
        currentScene = titleScene;
        SceneManager.getInstance().setScene(titleScene);
        disposeSplashScene();
    }
    
    
    private void disposeTitleScene()
    {
        ResourcesManager.getInstance().unloadTitleScreen();
        titleScene.disposeScene();
        titleScene = null;
        
    }
    
    public void createCreditsScene()
    {
        ResourcesManager.getInstance().loadCreditsResources();
        creditsScene = new SplashScene();
        SceneManager.getInstance().setScene(creditsScene);
        
    }
    
    
    private void disposeCreditsScene()
    {
        ResourcesManager.getInstance().unloadCreditsScreen();
        creditsScene.disposeScene();
        creditsScene = null;
        
    }
    
    /*
    public void createSoonScene()
    {
        ResourcesManager.getInstance().loadSoonResources();
        soonScene = new SplashScene();
        SceneManager.getInstance().setScene(soonScene);
    }
    */
    
    public void createMenuScene()
    {
    	ResourcesManager.getInstance().loadMenuResources();
        mainMenuScene = new MainMenuScene();
        SceneManager.getInstance().setScene(mainMenuScene);
        disposeTitleScene();
    }
    
    
    private void disposeMenuScene()
    {
        ResourcesManager.getInstance().unloadMenuScreen();
        mainMenuScene.disposeScene();
        mainMenuScene = null;
        
    }
    
    
    public void createSPMenuScene()
    {
    	ResourcesManager.getInstance().loadSPMenuResources();
        SPMenuScene = new SPMenuScene();
        loadingScene = new LoadingScene();
        SceneManager.getInstance().setScene(SPMenuScene);
        disposeMenuScene();
        
    }
    
    
    private void disposeSPMenuScene()
    {
        ResourcesManager.getInstance().unloadSPMenuScreen();
        SPMenuScene.disposeScene();
        SPMenuScene = null;
    }
    
    
 
    private void disposeMPGameScene()
    {
    	ResourcesManager.getInstance().unloadGameScreen();
        mpScene.disposeScene();
        mpScene = null;
        
        
    }
    
    public void createGameScene()
    {
    	ResourcesManager.getInstance().loadGameResources();
        classicScene = new ClassicScene();
        SceneManager.getInstance().setScene(classicScene);
        disposeSPMenuScene();
        
    }
    
    
    private void disposeGameScene()
    {
        ResourcesManager.getInstance().unloadGameScreen();
        classicScene.disposeScene();
        classicScene = null;
    }
    
    public void createOptionsScene()
    {
    	ResourcesManager.getInstance().loadOptionsResources();
        optionsScene = new OptionsScene();
        SceneManager.getInstance().setScene(optionsScene);
        disposeMenuScene();
        
    }
    
    
    private void disposeOptionsScene()
    {
        ResourcesManager.getInstance().unloadOptionsScreen();
        optionsScene.disposeScene();
        optionsScene = null;
    }
    
    public void createPlayerInfoScene()
    {
    	ResourcesManager.getInstance().loadPlayerInfoResources();
        playerInfoScene = new PlayerInfoScene();
        SceneManager.getInstance().setScene(playerInfoScene);
        disposeMenuScene();
        
    }
    
    
    private void disposePlayerInfoScene()
    {
        ResourcesManager.getInstance().unloadPlayerInfoScreen();
        playerInfoScene.disposeScene();
        playerInfoScene = null;
    }
    
    public void createGameOverScene()
    {
    	ResourcesManager.getInstance().loadGameOverResources();
        gameOverScene = new GameOverScene();
        SceneManager.getInstance().setScene(gameOverScene);
        disposeGameScene();
    }
    
    
    private void disposeGameOverScene()
    {
        ResourcesManager.getInstance().unloadGameOverScreen();
        gameOverScene.disposeScene();
        gameOverScene = null;
    }
    
    
    public void loadClassicGameScene(final Engine mEngine)
    {
        ResourcesManager.getInstance().unloadSPMenuTextures();
        mEngine.registerUpdateHandler(new TimerHandler(0.1f, new ITimerCallback() 
        {
            public void onTimePassed(final TimerHandler pTimerHandler) 
            {
                mEngine.unregisterUpdateHandler(pTimerHandler);
                ResourcesManager.getInstance().loadGameResources();
                classicScene = new ClassicScene();
                setScene(classicScene);
            }
            
        }));
        
    }
    
    public void loadArcadeGameScene(final Engine mEngine)
    {
        ResourcesManager.getInstance().unloadSPMenuTextures();
        mEngine.registerUpdateHandler(new TimerHandler(0.1f, new ITimerCallback() 
        {
            public void onTimePassed(final TimerHandler pTimerHandler) 
            {
                mEngine.unregisterUpdateHandler(pTimerHandler);
                ResourcesManager.getInstance().loadGameResources();
                arcadeScene = new ArcadeScene();
                setScene(arcadeScene);
            }
            
        }));
        
    }
    
    public void loadMultiplayerGameScene(final Engine mEngine)
    {
        ResourcesManager.getInstance().unloadMenuTextures();
        mEngine.registerUpdateHandler(new TimerHandler(0.1f, new ITimerCallback() 
        {
            public void onTimePassed(final TimerHandler pTimerHandler) 
            {
                mEngine.unregisterUpdateHandler(pTimerHandler);
                ResourcesManager.getInstance().loadMPGameResources();
                mpScene = new MultiplayerScene();
                setScene(mpScene);
            }
            
        }));
        
    }
    
    public void loadOptionsScene(final Engine mEngine)
    {
    	currentScene.disposeScene();
    	ResourcesManager.getInstance().unloadMenuTextures();
        mEngine.registerUpdateHandler(new TimerHandler(0.1f, new ITimerCallback() 
        {
            public void onTimePassed(final TimerHandler pTimerHandler) 
            {
                mEngine.unregisterUpdateHandler(pTimerHandler);
                ResourcesManager.getInstance().loadOptionsResources();
                optionsScene = new OptionsScene();
                setScene(optionsScene);
            }
            
        }));
        
    }
    
    public void loadPlayerInfoScene(final Engine mEngine)
    {
        ResourcesManager.getInstance().unloadMenuTextures();
        mEngine.registerUpdateHandler(new TimerHandler(0.1f, new ITimerCallback() 
        {
            public void onTimePassed(final TimerHandler pTimerHandler) 
            {
                mEngine.unregisterUpdateHandler(pTimerHandler);
                ResourcesManager.getInstance().loadPlayerInfoResources();
                playerInfoScene = new PlayerInfoScene();
                setScene(playerInfoScene);
            }
            
        }));
        
    }
    
    public void loadGameOverScene(final Engine mEngine)
    {
    	currentScene.disposeScene();
    	ResourcesManager.getInstance().unloadGameTextures();
        mEngine.registerUpdateHandler(new TimerHandler(0.1f, new ITimerCallback() 
        {
            public void onTimePassed(final TimerHandler pTimerHandler) 
            {
                mEngine.unregisterUpdateHandler(pTimerHandler);
                ResourcesManager.getInstance().loadGameOverResources();
                gameOverScene = new GameOverScene();
                setScene(gameOverScene);
            }
            
        }));
        
    }
    
    public void loadSoonScene(final Engine mEngine)
    {
        ResourcesManager.getInstance().unloadMenuTextures();
        mEngine.registerUpdateHandler(new TimerHandler(0.1f, new ITimerCallback() 
        {
            public void onTimePassed(final TimerHandler pTimerHandler) 
            {
                mEngine.unregisterUpdateHandler(pTimerHandler);
                ResourcesManager.getInstance().loadSoonResources();
                soonScene = new SoonScene();
                setScene(soonScene);
            }
            
        }));
        
    }
    
    public void loadCreditsScene(final Engine mEngine)
    {
        ResourcesManager.getInstance().unloadMenuTextures();
        mEngine.registerUpdateHandler(new TimerHandler(0.1f, new ITimerCallback() 
        {
            public void onTimePassed(final TimerHandler pTimerHandler) 
            {
                mEngine.unregisterUpdateHandler(pTimerHandler);
                ResourcesManager.getInstance().loadCreditsResources();
                creditsScene = new CreditsScene();
                setScene(creditsScene);
            }
            
        }));
        
    }
    
    public void loadAchievementsScene(final Engine mEngine)	//Evan will work on making this work
    {
        //setScene(loadingScene);
        ResourcesManager.getInstance().unloadMenuTextures();
        mEngine.registerUpdateHandler(new TimerHandler(0.1f, new ITimerCallback() 
        {
            public void onTimePassed(final TimerHandler pTimerHandler) 
            {
                mEngine.unregisterUpdateHandler(pTimerHandler);
                ResourcesManager.getInstance().loadAchievementsResources();
                achievementsScene = new AchievementsScene();
                setScene(achievementsScene);
            }
            
        }));
        
    }
    
    
    public void loadSPMenuScene(final Engine mEngine)
    {
    	
    	ResourcesManager.getInstance().unloadMenuTextures();
        mEngine.registerUpdateHandler(new TimerHandler(0.1f, new ITimerCallback() 
        {
            public void onTimePassed(final TimerHandler pTimerHandler) 
            {
                mEngine.unregisterUpdateHandler(pTimerHandler);
                ResourcesManager.getInstance().loadSPMenuResources();
                SPMenuScene = new SPMenuScene();
                setScene(SPMenuScene);
            }
            
        }));
        
    }
    
    
    public void loadTitleScene(final Engine mEngine)
    {
        currentScene.disposeScene();
        mEngine.registerUpdateHandler(new TimerHandler(0.1f, new ITimerCallback() 
        {
            public void onTimePassed(final TimerHandler pTimerHandler) 
            {
                mEngine.unregisterUpdateHandler(pTimerHandler);
                ResourcesManager.getInstance().loadTitleTextures();
                titleScene = new TitleScene();
                setScene(titleScene);
                
            }
            
        }));
        
    }
    
    public void loadMenuScene(final Engine mEngine)
    {
    	//currentScene.disposeScene();
    	ResourcesManager.getInstance().unloadTitleTextures();
    	
        mEngine.registerUpdateHandler(new TimerHandler(0.1f, new ITimerCallback() 
        {
            public void onTimePassed(final TimerHandler pTimerHandler) 
            {
                mEngine.unregisterUpdateHandler(pTimerHandler);
                ResourcesManager.getInstance().loadMenuTextures();
                mainMenuScene = new MainMenuScene();
                setScene(mainMenuScene);
                
            }
            
        }));
        
    }
    
    public void loadMenuScene(final Engine mEngine, SceneType type)
    {
        currentScene.disposeScene();
        
        switch (type)
        {
            case SCENE_SPMENU:
            	ResourcesManager.getInstance().unloadSPMenuTextures();
                break;
            case SCENE_ARCADE:
            	ResourcesManager.getInstance().unloadGameTextures();
                break;
            case SCENE_CLASSIC:
            	ResourcesManager.getInstance().unloadGameTextures();
                break;
            case SCENE_MULTIPLAYER:
            	ResourcesManager.getInstance().unloadMPGameTextures();
                break;
            case SCENE_OPTIONS:
            	ResourcesManager.getInstance().unloadOptionsTextures();
                break;
            case SCENE_SOON:
            	ResourcesManager.getInstance().unloadSoonTextures();
                break;
            case SCENE_ACHIEVEMENTS:
            	ResourcesManager.getInstance().unloadAchievementsTextures();
                break;
            case SCENE_INFO:
            	ResourcesManager.getInstance().unloadPlayerInfoTextures();
                break;
            case SCENE_GAMEOVER:
            	ResourcesManager.getInstance().unloadGameOverTextures();
                break;
            case SCENE_CREDITS:
            	ResourcesManager.getInstance().unloadCreditsTextures();
                break;
            case SCENE_TITLE:
            	ResourcesManager.getInstance().unloadTitleTextures();
                break;
            default:
                break;
                
        }
        
        mEngine.registerUpdateHandler(new TimerHandler(0.1f, new ITimerCallback() 
        {
            public void onTimePassed(final TimerHandler pTimerHandler) 
            {
                mEngine.unregisterUpdateHandler(pTimerHandler);
                ResourcesManager.getInstance().loadMenuTextures();
                mainMenuScene = new MainMenuScene();
                setScene(mainMenuScene);
                
            }
            
        }));
        
    }
    
    public static SceneManager getInstance()
    {
        return INSTANCE;
        
    }
    
    
    public SceneType getCurrentSceneType()
    {
        return currentSceneType;
        
    }
    
    
    public BaseScene getCurrentScene()
    {
        return currentScene;
        
    }
    
}